﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Moneyman.DataModel;
using Moneyman.Enums;
using Moneyman.Library;

namespace Moneyman.ViewModel
{
    public class TransactionsDailyViewModel : MainViewModel
    {
        private DateTime selectedDate = DateTime.Now;
        public DateTime SelectedDate
        {
            get { return selectedDate; }
            set
            {
                selectedDate = value;
                RaisePropertyChanged(() => SelectedDate);
                RaisePropertyChanged(() => DailyExpenseCollection);
                RaisePropertyChanged(() => DailyIncomeCollection);
                RaisePropertyChanged(() => DailyExpense);
                RaisePropertyChanged(() => DailyIncome);
            }
        }

        #region Count
        public double DailyIncome
        {
            get
            {
                if (DailyIncomeCollection.Count() > 0)
                {
                    return DailyIncomeCollection.Sum(t => t.Amount);
                }
                return 0;
            }
        }

        public double DailyExpense
        {
            get
            {
                if (DailyExpenseCollection.Count() > 0)
                {
                    return DailyExpenseCollection.Sum(t => t.Amount);
                }
                return 0;
            }
        }
        #endregion

        #region Collections
        public ObservableCollection<TransactionInfo> DailyIncomeCollection
        {
            get
            {
                DateTime start = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day);
                DateTime end = start.AddDays(1);
                IEnumerable<TransactionInfo> items = Database.CollectionOfTransactions.Where(t => t.Time >= start && t.Time < end && t.Kind == TransactionKindValue.Income);
                foreach (TransactionInfo item in items)
                {
                    item.Category = Database.CollectionOfCategories.FirstOrDefault(c => c.Guid == item.CategoryGuid);
                }
                return new ObservableCollection<TransactionInfo>(items);
            }
        }

        public ObservableCollection<TransactionInfo> DailyExpenseCollection
        {
            get
            {
                DateTime start = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day);
                DateTime end = start.AddDays(1);
                IEnumerable<TransactionInfo> items = Database.CollectionOfTransactions.Where(t => t.Time >= start && t.Time < end && t.Kind == TransactionKindValue.Expense);
                foreach (TransactionInfo item in items)
                {
                    item.Category = Database.CollectionOfCategories.FirstOrDefault(c => c.Guid == item.CategoryGuid);
                }
                return new ObservableCollection<TransactionInfo>(items);
            }
        } 
        #endregion

        #region Command
        public ICommand IncreaseDateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        SelectedDate = SelectedDate.AddDays(1);
                    }
                };
            }
        }

        public ICommand DecreaseDateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        SelectedDate = SelectedDate.AddDays(-1);
                    }
                };
            }
        }
        #endregion
    }
}
